<?php

/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2022/10/25 13:50
 * Desc:字节交易系统2.0
 */
class Controller_Pc_Api_V2_Toutiao_Trade extends Controller_Pc_Api_V2_Base
{

    //评论api控制器
    public function before()
    {
        parent::before();
        $token       = Common::remove_xss($this->request_data->token);
        $check_token = Model_Api_V2_Pub::token_decode($token);
        $this->mid   = $check_token['status'] ? $check_token['mid'] : '';
    }

    /**
     * 交易系统2.0下单请求参数
     */
    public function action_get_trade_options()
    {
        $ordersn = Common::remove_xss($this->request_data->ordersn);
        // 验证订单是否可以支付
        $checkOrder = Model_Api_V2_Payment::ordersn_checked($ordersn);
        if (!$checkOrder['status']) {
            $this->send_datagrams($this->client_info['id'], $checkOrder, $this->client_info['secret_key']);
        }
        $order_info = Model_Member_Order::order_info($ordersn);

        //"0元"订单,免费产品
        if ($order_info['pay_price'] == 0) {
            $zero_res = Model_Api_V2_Payment::zero_pay($ordersn);
            if ($zero_res['status']) {
                $sign                     = $zero_res['data']['sign'];
                $zero_res['data']         = $order_info;
                $zero_res['data']['sign'] = $sign;
            }
            $zero_res['status'] = false;
            $this->send_datagrams($this->client_info['id'], $zero_res, $this->client_info['secret_key']);
        }

        // 检查是否已经创建订单
        $code = Model_Toutiao_Pay_Log::need_create_pre_order($order_info);
        if ($code == 2) {
            $result = array(
                'status' => false,
                'msg' => '订单异常，请联系管理员'
            );
        } else if ($code == 1) {
            // 返回支付信息
            $result = array(
                'status' => true,
                'step' => 2,
                'data' => array(
                    'outOrderNo' => $ordersn
                ),
            );
        } else {
            // 获取预创建订单信息
            $trade_class = Model_Toutiao_Trade_Order_Factory::get_instance(2);
            $result      = $trade_class->get_pre_create_order($order_info);
            if ($result['status']) {
                $result['step'] = 1;
            }
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

    /**
     * 订单完成评价
     */
    public function action_rate()
    {
        try {

            if (!$this->mid) {
                throw new Exception('需登录');
            }

            $order_id = Common::remove_xss($this->request_data->order_id);
            //通过订单号码检测是否id是否合法
            $order = DB::select()
                ->from('member_order')
                ->where("id", "=", $order_id)
                ->where("memberid", "=", $this->mid)
                ->where("ispinlun", "=", 0)
                ->execute()
                ->current();
            if (empty($order)) {
                throw new Exception('订单不存在或已经评论');
            }

            $comment_model            = ORM::factory('comment');
            $comment_model->memberid  = $this->mid;
            $comment_model->content   = '抖音用户评价';
            $comment_model->orderid   = $order_id;
            $comment_model->typeid    = $order['typeid'];
            $comment_model->articleid = $order['productautoid'];
            $comment_model->level     = 5; // 默认好评
            $comment_model->piclist   = '';
            $comment_model->addtime   = time();
            $comment_model->save();
            if ($comment_model->saved()) {

                // 积分赠送
                if ($order['jifencomment']) {
                    Model_Member::operate_jifen($this->mid, $order['jifencomment'], 2);
                    Product::add_jifen_log($this->mid, "评论赠送积分{$order['jifencomment']}分", $order['jifencomment'], 2);
                }

                //修改订单为已评价
                $order_model           = ORM::factory('member_order', $order_id);
                $order_model->ispinlun = 1;
                $order_model->save();
                $result = array('status' => true, 'msg' => '');

            } else {
                throw new Exception(__("save_failure"));
            }

        } catch (Exception $e) {
            $result = array('status' => false, 'msg' => $e->getMessage());
        }
        $this->send_datagrams($this->client_info['id'], $result, $this->client_info['secret_key']);
    }

}